import axios, { type AxiosRequestConfig, type RawAxiosRequestHeaders } from 'axios';
import { useUserStore } from '@/stores/user';
const service = axios.create({
  // baseURL: import.meta.env.VITE_API_URL,
  timeout: 5000,
});

service.interceptors.request.use(
  (config: AxiosRequestConfig<any>) => {
    //请求头中携带token
    const userStore = useUserStore()
    let token = userStore.token
    // console.log(token)
    if (token) {
      (config.headers as RawAxiosRequestHeaders).token = token
    }
    let keywordPrefix = localStorage.getItem('keywordPrefix');
    if(keywordPrefix){
      config.headers={
        'Content-Type':'application/x-www-form-urlencoded'
      };
      config.headers.charset = 'UTF-8'
    }
    return config;
  },err => {
    return Promise.reject(err)
  }
);

service.interceptors.response.use(res => {
  return res.data
}, err => {
  return Promise.reject(err)
})

export default service